package vaccine.appointment.system.statistics.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import vaccine.appointment.system.common.annotation.AutoWrapResult;
import vaccine.appointment.system.common.annotation.RequireRole;
import vaccine.appointment.system.statistics.service.StatisticsService;
import vaccine.appointment.system.statistics.vo.AppointmentTrendVO;
import vaccine.appointment.system.statistics.vo.SiteStatisticsVO;
import vaccine.appointment.system.statistics.vo.SystemOverviewVO;
import vaccine.appointment.system.user.enums.IdentityInfoEnum;

import java.time.LocalDate;
import java.util.List;

/**
 * 管理员统计控制器
 */
@AutoWrapResult
@RequestMapping("/admin/statistics")
@RestController
@RequireRole({IdentityInfoEnum.HOSPITAL_ADMIN, IdentityInfoEnum.SUPER_ADMIN})
public class AdminStatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    /**
     * 获取系统概览统计
     * @return 系统概览统计数据
     */
    @GetMapping("/overview")
    @RequireRole({IdentityInfoEnum.SUPER_ADMIN})
    public SystemOverviewVO getSystemOverview() {
        return statisticsService.getSystemOverview();
    }

    /**
     * 获取预约趋势统计
     * @param startDate 开始日期（默认为7天前）
     * @param endDate 结束日期（默认为今天）
     * @return 预约趋势列表
     */
    @GetMapping("/appointment-trend")
    public List<AppointmentTrendVO> getAppointmentTrend(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        // 默认查询最近7天的数据
        if (startDate == null) {
            startDate = LocalDate.now().minusDays(6);
        }
        if (endDate == null) {
            endDate = LocalDate.now();
        }
        
        return statisticsService.getAppointmentTrend(startDate, endDate);
    }

    /**
     * 获取特定接种点的统计数据
     * @param siteId 接种点ID
     * @return 接种点统计数据
     */
    @GetMapping("/site/{siteId}")
    public SiteStatisticsVO getSiteStatistics(@PathVariable Integer siteId) {
        return statisticsService.getSiteStatistics(siteId);
    }
}

